package com.qf.custom;


import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author lixu
 */
public class MySerializer extends JsonSerializer<Double> {


    /**
     *
     * @param value User 工资
     * @param gen Generator used to output resulting Json content
     * @param serializers Provider that can be used to get serializers for
     *   serializing Objects value contains, if any.
     * @throws IOException
     */
    @Override
    public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) throws IOException {

        //BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_HALF_UP);
        //关于钱的：
        BigDecimal bigDecimal = new BigDecimal(value);
        BigDecimal v = bigDecimal.setScale(2, RoundingMode.HALF_UP);
        //写回到User对象的成员变量上
        gen.writeNumber(v);
    }
}
